package com.communal.web;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.communal.service.IAuthService;
import com.communal.util.AppException;

/**
 * 
 * @author Created by andrey.kuznetsov <br>
 * @author Last modified by $Author$ <br>
 * @author Last modified on $Date$ at revision $Revision$ <br>
 */
@Controller
public class AuthController extends AbstractController{

	private final String INIT_APP_ACTION = "/init.action";
	private final String PING_APP_ACTION = "/auth/ping.action";
	private final String USER_INFO_ACTION = "/auth/info.action";
	private final String USER_SAVE_PROFILE_ACTION = "/auth/saveprofile.action";
	
	private final String REGISTER_ACTION = "/authUser/registration.do";
	private final String MAIN_JSP_PAGE = "main";
	
	@Autowired
	protected IAuthService authService;
	
	@RequestMapping(value=INIT_APP_ACTION)
	public ModelAndView initApplication(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		return new ModelAndView(MAIN_JSP_PAGE);
	}
	
	@RequestMapping(value=USER_INFO_ACTION)
	public @ResponseBody Map<String, ? extends Object> calculateBalance() throws Exception {
		try{
			return getMap(userService.getUserInfoForCurrentLoggedUser());
		} catch (AppException error) {
			return getModelMapError(error.getMessage());
		}
	}
	
	@RequestMapping(value=USER_SAVE_PROFILE_ACTION, method=RequestMethod.POST)
	public @ResponseBody Map<String, ? extends Object> saveUserProfile(
			@RequestParam (value = "j_username", required = true ) String login,
			@RequestParam (value = "j_password", required = true ) String password,
			@RequestParam (value = "firstName", required = true ) String firstName,
			@RequestParam (value = "lastName", required = true ) String lastName) throws Exception {
		try{
			userService.saveUserProfile( 
					userService.getUserInfoForCurrentLoggedUser().getId(), login, password, firstName, lastName );
			Map<String,Object> modelMap = new HashMap<String, Object>();
			modelMap.put(KEY_SUCCESS, true);
			return modelMap;
		}catch (AppException error) {
			return getModelMapError(error.getMessage());
		}
	}
	
	@RequestMapping(value=PING_APP_ACTION)
	public @ResponseBody Map<String, ? extends Object> pingApplication(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		try{
			userService.getModelForCurrentLoggedUser();
			Map<String,Object> modelMap = new HashMap<String, Object>();
			modelMap.put(KEY_SUCCESS, true);
			return modelMap;
		}catch (AppException err) {
			return getModelMapError(err.getMessage());
		}
	}
	
	@RequestMapping(value=REGISTER_ACTION, method=RequestMethod.POST)
	public @ResponseBody Map<String, ? extends Object> registerNewUser(
			@RequestParam (value = "j_username", required = true ) String login,
			@RequestParam (value = "j_password", required = true ) String password,
			@RequestParam (value = "firstName", required = true ) String firstName,
			@RequestParam (value = "lastName", required = true ) String lastName) throws Exception {
		try{
			userService.registerNewUser( login, password, firstName, lastName );
			Map<String,Object> modelMap = new HashMap<String, Object>();
			modelMap.put(KEY_SUCCESS, true);
			return modelMap;
		}catch (AppException error) {
			return getModelMapError(error.getMessage());
		}
	}
}
